//
//  NetWorkManager.m
//  liwushuo
//
//  Created by qf1 on 16/4/15.
//  Copyright (c) 2016年 ZJH. All rights reserved.
//

#import "NetWorkManager.h"
#import "AFNetworking.h"

@implementation NetWorkManager

- (void)getNavigationNameAndBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    NSMutableArray * dataArray = [[NSMutableArray alloc] init];
    [manager GET:@"http://api.liwushuo.com/v2/channels/preset?gender=1&generation=2" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSArray * navigationArray = [[responseObject objectForKey:@"data"] objectForKey:@"candidates"];
        for (NSDictionary * dict in navigationArray) {
            NavigationItem * item = [[NavigationItem alloc] init];
            item.name = [dict objectForKey:@"name"];
            item.ID = [dict objectForKey:@"id"];
            [dataArray addObject:item];
        }
        block(dataArray);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"获取失败");
    }];
}

- (void)getContentDataWithUrl:(NSString *)url andBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * array = [[NSMutableArray alloc] init];
        NSArray * arr = [[responseObject objectForKey:@"data"] objectForKey:@"items"];
        NSRange range = [url rangeOfString:@"offset=0"];
        if (range.length != 0) {
            [array addObject:[[[responseObject objectForKey:@"data"]objectForKey:@"paging"] objectForKey:@"next_url"]];
        }
        
        for (NSDictionary * dict in arr) {
            ContentItem * item = [[ContentItem alloc] init];
            [item setValuesForKeysWithDictionary:dict];
            [array addObject:item];
        }
        block(array);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)getAdvDataAndBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:@"http://api.liwushuo.com/v2/banners" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"banners"];
        for (NSDictionary * dict in array) {
            if ([dict objectForKey:@"target"] != NULL) {
                AdvItem * item = [[AdvItem alloc] init];
                [item setValuesForKeysWithDictionary:dict];
                [dataArray addObject:item];
            }
        }
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}

- (void)getTopicDataAndBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:@"http://api.liwushuo.com/v2/secondary_banners?gender=1&generation=2" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"secondary_banners"];
        for (NSDictionary * dict in array) {
            TopicItem * item = [[TopicItem alloc] init];
            [item setValuesForKeysWithDictionary:dict];
            NSRange range1 = [item.target_url rangeOfString:@"topic_id="];
            if (range1.length != 0){
                [dataArray addObject:item];
            }
            
        }
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
}

- (void)getJXContentDataWithUrl:(NSString *)url andBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"items"];
        for (NSDictionary * dict in array) {
            if ([dict valueForKey:@"ad_type"]) {
//                AdvContentItem * item = [[AdvContentItem alloc] init];
//                [item setValuesForKeysWithDictionary:dict];
//                [dataArray addObject:item];
            }else{
                ContentItem * item = [[ContentItem alloc] init];
                [item setValuesForKeysWithDictionary:dict];
                [dataArray addObject:item];
            }
            
        }
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)getAdvContentDataWithUrl:(NSString *)url andBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        NSArray * array = [[responseObject objectForKey:@"data"]objectForKey:@"posts"];
        for (NSDictionary * dict in array) {
            ContentItem * item = [[ContentItem alloc] init];
            [item setValuesForKeysWithDictionary:dict];
            [dataArray addObject:item];
        }
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"fail");
    }];
}

- (void)getHotDataWithUrl:(NSString *)url andBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"items"];
        for (NSDictionary * dataDict in array) {
            HotItem * item = [[HotItem alloc] init];
            if ([dataDict objectForKey:@"id"]) {
                [item setValuesForKeysWithDictionary:dataDict];
            }else{
                NSDictionary * dict = [dataDict objectForKey:@"data"];
                [item setValuesForKeysWithDictionary:dict];
            }
            [dataArray addObject:item];
        }
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)getDetailDataWithUrl:(NSString *)url andBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * dict = [responseObject objectForKey:@"data"];
        DetailItem * item = [[DetailItem alloc] init];
        [item setValuesForKeysWithDictionary:dict];
        block(item);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)getCategoryTopicDataWithUrl:(NSString *)url andBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"collections"];
        for (NSDictionary * dict in array) {
            CategoryTopicItem * item = [[CategoryTopicItem alloc] init];
            [item setValuesForKeysWithDictionary:dict];
            [dataArray addObject:item];
        }
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)getCategoryDataWithUrl:(NSString *)url andBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"channel_groups"];
        for (NSDictionary * dict in array) {
            NSMutableArray * dictArray = [[NSMutableArray alloc] init];
            NSArray * contentArray = [dict objectForKey:@"channels"];
            for (NSDictionary * contentDict in contentArray) {
                CategoryItem * item = [[CategoryItem alloc] init];
                [item setValuesForKeysWithDictionary:contentDict];
                [dictArray addObject:item];
            }
            [dictArray addObject:[dict objectForKey:@"name"]];
            [dataArray addObject:dictArray];
        }
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)getCategorySecondDataWithUrl:(NSString *)url andBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"categories"];
        for (NSDictionary * dict in array) {
            NSMutableArray * contentArray = [[NSMutableArray alloc] init];
            [contentArray addObject:dict[@"name"]];
            NSArray * arr = dict[@"subcategories"];
            for (NSDictionary * contentDict in arr) {
                SecondCategoryItem * item = [[SecondCategoryItem alloc] init];
                [item setValuesForKeysWithDictionary:contentDict];
                [contentArray addObject:item];
            }
            [dataArray addObject:contentArray];
        }
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)getSelectContentDataWithUrl:(NSString *)url andBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"items"];
        for (NSDictionary * dict in array) {
            SelectItem * item = [[SelectItem alloc] init];
            [item setValuesForKeysWithDictionary:dict];
            [dataArray addObject:item];
        }
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)getSelectDataWithUrl:(NSString *)url andBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"filters"];
        for (NSDictionary * dict in array) {
            NSMutableArray * contentArray = [[NSMutableArray alloc] init];
            NSArray * arr = dict[@"channels"];
            [contentArray addObject:dict[@"name"]];
            for (NSDictionary * contentDict in arr) {
                SelectNavigationItem * item = [[SelectNavigationItem alloc] init];
                [item setValuesForKeysWithDictionary:contentDict];
                [contentArray addObject:item];
            }
            [contentArray addObject:dict[@"key"]];
            [dataArray addObject:contentArray];
        }
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)getSearchHotNameWithblock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://api.liwushuo.com/v2/search/hot_words" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray * dataArray = [[responseObject objectForKey:@"data"] objectForKey:@"hot_words"];
        block(dataArray);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"fail");
    }];
}

- (void)getSearchPresentDataWithUrl:(NSString *)url andBlock:(networkResponst)block{
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"items"];
        for (NSDictionary * dict in array) {
            SearchItem * item = [[SearchItem alloc] init];
            [item setValuesForKeysWithDictionary:dict];
            [dataArray addObject:item];
        }
        block(dataArray);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"fail");
    }];
}

- (void)getSearchTacticDataWithUrl:(NSString *)url andBlock:(networkResponst)block{
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"posts"];
        for (NSDictionary * dict in array) {
            SearchItem * item = [[SearchItem alloc] init];
            [item setValuesForKeysWithDictionary:dict];
            [dataArray addObject:item];
        }
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"fail");
    }];
}

- (void)getCommentDataWithUrl:(NSString *)url andBlock:(networkResponst)block{
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray * dataArray = [[NSMutableArray alloc] init];
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"comments"];
        for (NSDictionary * dict in array) {
            CommentItem * item = [[CommentItem alloc] init];
            [item setValuesForKeysWithDictionary:dict];
            [dataArray addObject:item];
        }
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"fail");
    }];
}

- (void)getPersonMessageWithCookie:(NSString *)cookie andBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
    
    [manager GET:@"http://api.liwushuo.com/v2/users/me" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary * dict = [responseObject objectForKey:@"data"];
        PersonItem * item = [[PersonItem alloc] init];
        [item setValuesForKeysWithDictionary:dict];
        block(item);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"fail");
    }];
}

- (void)getPersonPresentWithCookie:(NSString *)cookie andBlock:(networkResponst)block{
    NSMutableArray * dataArray = [[NSMutableArray alloc] init];
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
    
    [manager GET:@"http://api.liwushuo.com/v2/users/me/favorite_lists?limit=20&offset=0" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"favorite_lists"];
        for (NSDictionary * dict in array) {
            PersonPresentItem * item = [[PersonPresentItem alloc] init];
            [item setValuesForKeysWithDictionary:dict];
            [dataArray addObject:item];
        }
        
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"fail");
    }];
}

- (void)getPersonTacticWithCookie:(NSString *)cookie andBlock:(networkResponst)block{
    NSMutableArray * dataArray = [[NSMutableArray alloc] init];
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
    
    [manager GET:@"http://api.liwushuo.com/v2/users/me/post_likes?limit=20&offset=0" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray * array = [[responseObject objectForKey:@"data"] objectForKey:@"posts"];
        for (NSDictionary * dict in array) {
            ContentItem * item = [[ContentItem alloc] init];
            [item setValuesForKeysWithDictionary:dict];
            [dataArray addObject:item];
        }
        
        block(dataArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"fail");
    }];
}

- (void)sendCommentWithId:(NSString *)Id AndCookie:(NSString *)cookie andComment:(NSString *)comment AndBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
    NSDictionary * dict = @{@"content":comment};
    [manager POST:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/posts/%@/comments",Id] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"fail");
    }];
}

- (void)changeNickNameWithName:(NSString *)name andCookie:(NSString *)cookie AndBlock:(networkResponst)block{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
    NSDictionary * dict = @{@"nickname":name};
    [manager PUT:@"http://api.liwushuo.com/v2/users/me" parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"fail");
    }];
    
}

@end
